//
//  BVMineAlertView.m
//  XiangYu
//
//  Created by YYKJ on 2017/6/9.
//  Copyright © 2017年 YYKJ. All rights reserved.
//  单列弹框

#import "BVMineSingleAlertView.h"
// AlertW 宽
#define AlertW [UIScreen mainScreen].bounds.size.width-80

@interface BVMineSingleAlertView ()<UIPickerViewDelegate,UIPickerViewDataSource>

/** 弹窗 */
@property(nonatomic ,retain) UIView *alertView;

@property (nonatomic ,strong) UIView         *upLine;
@property (nonatomic ,strong) UIButton       *upLeftBtn;
@property (nonatomic ,strong) UIButton       *upRightBtn;
@property (nonatomic ,strong) UIPickerView   *pickView;

/***  本人性别*/
@property (nonatomic ,strong) NSArray *sexArr;
/***  本人身高*/
@property (nonatomic ,strong) NSMutableArray *heightArr;
/***  本人学历*/
@property (nonatomic ,strong) NSArray *educationArr;
/***  本人月薪*/
@property (nonatomic ,strong) NSArray *MonthlySalaryArr;
/***  本人公司类型*/
@property (nonatomic ,strong) NSArray *companyTypeArr;
/***  本人工作状态*/
@property (nonatomic ,strong) NSArray *workingStateArr;
/***  本人锻炼习惯*/
@property (nonatomic ,strong) NSArray *exerciseArr;
/***  本人是否抽烟*/
@property (nonatomic ,strong) NSArray *smokeArr;
/***  本人是否喝酒*/
@property (nonatomic ,strong) NSArray *drinkArr;
/***  本人是否和父母同住*/
@property (nonatomic ,strong) NSArray *liveParentsArr;
/***  本人是否和父母同住*/
@property (nonatomic ,strong) NSArray *faithArr;

/***  择偶_诚信星级*/
@property (nonatomic ,strong) NSArray *spouse_goodFaithArr;
/***  择偶_信仰*/
@property (nonatomic ,strong) NSArray *spouse_faithArr;
/***  择偶_婚姻状况*/
@property (nonatomic ,strong) NSArray *spouse_maritalStatusArr;

/***  临时数组*/
@property (nonatomic ,strong) NSMutableArray *tempArr;

@end

@implementation BVMineSingleAlertView

- (instancetype)init:(WorkMethod)method{
    
    if (self = [super init]) {
        
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithRed:110/255.0 green:110/255.0 blue:110/255.0 alpha:0.5];
        self.alertView = [[UIView alloc]init];
        [self addSubview:self.alertView];
        [self.alertView setBackgroundColor:[UIColor whiteColor]];
        [self.alertView setFrame:CGRectMake(0, 0, AlertW, 200)];
        [self.alertView.layer setCornerRadius:5.0];
        [self.alertView.layer setPosition:self.center];
        
        self.upLeftBtn = [self upBtn];
        [self.alertView addSubview:self.upLeftBtn];
        [self.upLeftBtn setFrame: CGRectMake(10, 10, 50, 25)];
        [self.upLeftBtn setTitle:@"取消" forState:UIControlStateNormal];
        [self.upLeftBtn setTag:100];
        
        self.upRightBtn = [self upBtn];
        [self.alertView addSubview:self.upRightBtn];
        [self.upRightBtn setFrame:CGRectMake(AlertW-50-10, 10, 50, 25)];
        [self.upRightBtn setTitle:@"确认" forState:UIControlStateNormal];
        [self.upRightBtn setTag:200];
        
        self.upLine = [[UIView alloc]initWithFrame:CGRectMake(0,40,AlertW,1.0)];
        [self.upLine setBackgroundColor:[UIColor BVLineGrayColor]];
        [self.alertView addSubview:self.upLine];
        
        [self layoutUI:method];
        
    }
    return self;
}

-(void)layoutUI:(WorkMethod)method{
    
    switch (method) {
        case Sex:{
            self.tempArr = (NSMutableArray *)self.sexArr;
            
        }
            break;
        case MonthlySalary:{
            self.tempArr = (NSMutableArray *)self.MonthlySalaryArr;
            
        }
            break;
        case Education:{
            self.tempArr = (NSMutableArray *)self.educationArr;
            
        }
            break;
        case height:{
            self.tempArr = self.heightArr;
            
        }
            break;
        case CompanyType:{
            self.tempArr = (NSMutableArray *)self.companyTypeArr;
            
        }
            break;
        case WorkingState:{
            self.tempArr = (NSMutableArray *)self.workingStateArr;
            
        }
            break;
        case Exercise:{
            self.tempArr = (NSMutableArray *)self.exerciseArr;
            
        }
            break;
        case Smoke:{
            self.tempArr = (NSMutableArray *)self.smokeArr;
            
        }
            break;
        case Drink:{
            self.tempArr = (NSMutableArray *)self.drinkArr;
            
        }
            break;
        case LiveParents:{
            self.tempArr = (NSMutableArray *)self.liveParentsArr;
            
        }
            break;
        case Faith:{
            self.tempArr = (NSMutableArray *)self.faithArr;
            
        }
            break;
        case Spouse_height:{
            self.tempArr = (NSMutableArray *)self.heightArr;
            
        }
            break;
        case Spouse_education:{
            self.tempArr = (NSMutableArray *)self.educationArr;
            
        }
            break;
        case Spouse_marriage:{
            self.tempArr = (NSMutableArray *)self.spouse_maritalStatusArr;
            
        }
            break;
        case Spouse_housePurchase:{
            self.tempArr = (NSMutableArray *)self.liveParentsArr;
            
        }
            break;
        case Spouse_carPurchase:{
            self.tempArr = (NSMutableArray *)self.liveParentsArr;
            
        }
            break;
        case Spouse_monthlySalary:{
            self.tempArr = (NSMutableArray *)self.MonthlySalaryArr;
            
        }
            break;
        case Spouse_liveParents:{
            self.tempArr = (NSMutableArray *)self.liveParentsArr;
            
        }
            break;
        case Spouse_wantChild:{
            self.tempArr = (NSMutableArray *)self.liveParentsArr;
            
        }
            break;
        default:
            break;
    }
    
    self.pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 50, self.alertView.width, self.alertView.height-60)];
    [self.alertView addSubview:self.pickView];
    self.pickView.delegate = self;
    self.pickView.dataSource = self;

    //[self.pickView selectRow:1 inComponent:0 animated:YES];
    
}

#pragma mark - UIPickerViewDataSource
// 有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 1;
}

// 有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    return self.tempArr.count;
    
}

#pragma mark - UIPickerViewDelegate
// 告诉系统每一行显示什么内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    return [NSString stringWithFormat:@"%@",self.tempArr[row]];

}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setFont:[UIFont systemFontOfSize:22]];
        [pickerLabel setTextColor:[UIColor BVTextDarkGrayColor]];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

-(void)disBtnClick:(UIButton *)btn{
    if (btn.tag == 100) {
        [self removeFromSuperview];
    }else{
        // 返回拨键盘时被选定的行
        NSString *str = self.tempArr[[self.pickView selectedRowInComponent:0]];
        if (self.AlertResult) {
            self.AlertResult(str);
        }
        [self removeFromSuperview];
    }
    
}

#pragma mark - 弹出
-(void)showMKPAlertView{
    
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [self creatShowAnimation];
}

-(void)creatShowAnimation
{
    self.alertView.layer.position = self.center;
    self.alertView.transform = CGAffineTransformMakeScale(0.90, 0.90);
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
       
    }];
    
}

-(UIButton *)upBtn{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(disBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [btn setTitleColor:[UIColor BVBaseOrangeColor]forState:UIControlStateNormal];
    [btn.layer setCornerRadius:3.0];
    [btn.layer setMasksToBounds:YES];
    [btn.layer setBorderWidth:1.0];
    [btn.layer setBorderColor:[UIColor BVLineGrayColor].CGColor];
    
    return btn;
}

-(NSArray *)sexArr{
    if (!_sexArr) {
        _sexArr = [NSArray arrayWithObjects:@"男",@"女", nil];
    }
    return _sexArr;
}

-(NSMutableArray *)heightArr{
    if (!_heightArr) {
        _heightArr = [NSMutableArray array];
        for (int i = 150; i < 201; i++) {
            [_heightArr addObject:[NSString stringWithFormat:@"%dcm",i]];
        }
        [_heightArr addObject:@"200以上"];
    }
    return _heightArr;
}

-(NSArray *)educationArr{
    if (!_educationArr) {
        _educationArr = [NSArray arrayWithObjects:@"高中以下",@"高中",@"专科",@"本科",@"硕士",@"博士", nil];
    }
    return _educationArr;
}

-(NSArray *)MonthlySalaryArr{
    if (!_MonthlySalaryArr) {
        _MonthlySalaryArr = [NSArray arrayWithObjects:
                                  @"2000以下",@"2000 - 300",@"3000 - 4000",
                                   @"4000 - 5000",@"5000 - 6000",@"6000 - 7000",
                                   @"7000 - 8000",@"8000 - 9000",@"9000 - 10000",
                                   @"10000 - 15000",@"15000 - 20000",@"20000 - 3000",
                                   @"30000 - 50000",@"50000 - 10000",@"10万以上",nil];
    }
    return _MonthlySalaryArr;
}

-(NSArray *)companyTypeArr{
    if (!_companyTypeArr) {
        _companyTypeArr = [NSArray arrayWithObjects:@"国有",@"民营", nil];
    }
    return _companyTypeArr;
}

-(NSArray *)workingStateArr{
    if (!_workingStateArr) {
        _workingStateArr = [NSArray arrayWithObjects:@"在职",@"离职", nil];
    }
    return _workingStateArr;
}

-(NSArray *)exerciseArr{
    if (!_exerciseArr) {
        _exerciseArr = [NSArray arrayWithObjects:@"每周一次",@"每周两次",@"每周三次",
                                                @"每月一次",@"每月两次",@"每月三次",nil];
    }
    return _exerciseArr;
}
-(NSArray *)smokeArr{
    if (!_smokeArr) {
        _smokeArr = [NSArray arrayWithObjects:@"是",@"否",@"偶尔",nil];
    }
    return _smokeArr;
}

-(NSArray *)drinkArr{
    if (!_drinkArr) {
        _drinkArr = [NSArray arrayWithObjects:@"是",@"否",@"偶尔",nil];
    }
    return _drinkArr;
}

-(NSArray *)liveParentsArr{
    if (!_liveParentsArr) {
        _liveParentsArr = [NSArray arrayWithObjects:@"是",@"否",nil];
    }
    return _liveParentsArr;
}

-(NSArray *)faithArr{
    if (!_faithArr) {
        _faithArr = [NSArray arrayWithObjects:@"佛教",@"伊斯兰教",@"其他", nil];
    }
    return _faithArr;
}

-(NSArray *)spouse_maritalStatusArr{
    if (!_spouse_maritalStatusArr) {
        _spouse_maritalStatusArr = [NSArray arrayWithObjects:@"已婚",@"未婚",@"离异", nil];
    }
    return _spouse_maritalStatusArr;
}

@end
